/*
 * Demoiselle Framework
 * Copyright (C) 2011 SERPRO
 * ----------------------------------------------------------------------------
 * This file is part of Demoiselle Framework.
 * 
 * Demoiselle Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not,  see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 * ----------------------------------------------------------------------------
 * Este arquivo é parte do Framework Demoiselle.
 * 
 * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
 * modificá-lo dentro dos termos da Licença Pública Geral GNU como publicada pela Fundação
 * do Software Livre (FSF); na versão 2 da Licença.
 * 
 * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
 * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
 * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/GPL em português
 * para maiores detalhes.
 * 
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU, sob o título
 * "LICENCA.txt", junto com esse programa. Se não, acesse o Portal do Software Público
 * Brasileiro no endereço www.softwarepublico.gov.br ou escreva para a Fundação do Software
 * Livre (FSF) Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
 
 * @author Tarcizio Vieira Neto (tarcizio.vieira@owasp.org) <a href="http://www.serpro.gov.br">SERPRO</a>
 * @created 2011
 */

package br.gov.frameworkdemoiselle.security.dependencies.shiro;

import java.util.Set;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.subject.Subject;
import org.owasp.esapi.User;
import org.owasp.esapi.errors.AuthenticationException;
import org.owasp.esapi.errors.EncryptionException;
import org.owasp.esapi.reference.AbstractAuthenticator;


public class ShiroAuthenticator extends AbstractAuthenticator implements br.gov.frameworkdemoiselle.security.Authenticator {
	

	private static final long serialVersionUID = 1L;

	private UsernamePasswordCredential credential;

	private Subject subject;

	private br.gov.frameworkdemoiselle.security.User user;

	@Inject
	public ShiroAuthenticator(Subject subject, UsernamePasswordCredential credential) {
		this.subject = subject;
		this.credential = credential;
		init();
	}

	private void init() {
		user = new br.gov.frameworkdemoiselle.security.User() {

			private static final long serialVersionUID = 1L;

			@Override
			public String getId() {
				return subject.getPrincipal().toString();
			}

			@Override
			public Object getAttribute(Object key) {
				return subject.getSession().getAttribute(key);
			}

			@Override
			public void setAttribute(Object key, Object value) {
				subject.getSession().setAttribute(key, value);
			}
		};
	}
	
	@Override
	public br.gov.frameworkdemoiselle.security.User getUser() {
		return user;
	}


	@Override
	public boolean authenticate() {
		boolean authenticated = false;

		try {
			subject.login(credential.getDelegate());
			authenticated = true;
		} catch (Exception cause) {
			//cause.printStackTrace();
			// TODO log.info("There is no user with username of " + token.getPrincipal());
		}

		return authenticated;
	}

	@Override
	public void unAuthenticate() {
		subject.logout();
	}

	@Override
	public boolean verifyPassword(User user, String password) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User createUser(String accountName, String password1, String password2) throws AuthenticationException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String generateStrongPassword() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String generateStrongPassword(User user, String oldPassword) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void changePassword(User user, String currentPassword, String newPassword, String newPassword2)
			throws AuthenticationException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User getUser(long accountId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User getUser(String accountName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Set getUserNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String hashPassword(String password, String accountName) throws EncryptionException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void removeUser(String accountName) throws AuthenticationException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void verifyAccountNameStrength(String accountName) throws AuthenticationException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void verifyPasswordStrength(String oldPassword, String newPassword, User user)
			throws AuthenticationException {
		// TODO Auto-generated method stub
		
	}
}
